<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Admin_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 根据用户名和密码来得到管理员账号
	*/
	public function get_admin($username, $password) {
		$query = $this->db->get_where ( 't_admin', array (
				'username' => $username,
				'password' => $password 
		) );
		if ($query->num_rows () > 0) {
			return $query->row_array();
		}
		return array();
	}
	/*
	 * 返回所有管理员账号
	 */
	public function find_all_admins(){
		$query=$this->db->get('t_admin');
		return $query->result_array();
	}
	/*
	 * 根据管理员ID得到管理员账号信息
	 */
	public function load($id){
		$query = $this->db->get_where('t_admin',array('id' => $id));
		if ($row = $query->row_array()){
			return $row;
		}
		return array();
	}
	/*
	 *添加管理员账号信息
	 */
	public function create($admin){
		$this->db->insert('t_admin', $admin);
		return $this->db->insert_id();
	}
	/*
	 *更新管理员账号信息
	 */
	public function update($id,$admin){
		$this->db->where('id', $id);
		$this->db->update('t_admin', $admin);
		return $this->db->affected_rows()>0;
	}
	/*
	 *删除管理员账号信息
	 */
	public function delete($id){
		$this->db->where('id', $id);
		$this->db->delete('t_admin');
		return $this->db->affected_rows()>0;
	}
	/*
	 * 根据token查找管理员
	 */
	public function find_by_token($token){
		$this->db->where('token', $token);
		$query = $this->db->get('t_admin');
		return $query->row_array();
	}
}